<?php
/*
Copyright 2007,2008 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


require_once('BH_Widget_Base.php');

class BH_Recent_Entries_Widget extends BH_Controllable_Widget_Base {
	function widget($args) {
		extract($args);
		
		$style = $this->get_attribute('style');
		$title = $this->get_attribute('title');
		$show_posts = $this->get_attribute('show_posts');
		$show_posts = is_numeric($show_posts) ? ($show_posts > 0 ? $show_posts : 0) : 5;

		if ($show_posts <=  0) { return; }

		$r = new WP_Query('showposts='. $show_posts);
		if (!$r->have_posts())  return;

		echo $before_widget;
		echo $this->get_widget_start_tag($args);
		
		echo $before_title . $title . $after_title;
		echo $before_list;

		while ($r->have_posts()) {
			$r->the_post(); 
			echo $before_item;
			echo '<a href="'. get_permalink() . '">' . ( get_the_title() ? the_title('','',false) : $post->ID ) .'</a></li>';
			echo $after_item;
		}

		echo $after_list;
		
		echo $this->get_widget_end_tag($args);
		echo $after_widget; 
	}

	function control () {
		if ( $this->get_request_param_value('submit') ) {
			$this->update_attribute_from_param('title');
			$this->update_attribute_from_param('show-posts');
		}
		
		$title = $this->get_attribute('title');
		$show_posts = $this->get_attribute('show-posts');
		$show_posts = is_numeric($show_posts) ? ($show_posts > 0 ? $show_posts : 0) : 5;

		echo "<label>Title:</label><input name=\"". $this->get_request_param_name('title') ."\" value=\"". $title ."\"/><br/>\n";
		echo "<label>Show Posts:</label><input name=\"". $this->get_request_param_name('show-posts') ."\" value=\"". $show_posts ."\"/><br/>\n";
		echo "<input type=\"hidden\" name=\"". $this->get_request_param_name('submit') ."\" value=\"1\"/><br/>\n";
	}
}

bh_widgets_register_class('BH_Recent_Entries_Widget');
?>
